using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

/// <summary>
/// Battle场景管理器 - 确保所有必要组件正确初始化和连接
/// </summary>
public class BattleSceneManager : MonoBehaviour
{
    // UI元素
    public Text battleMessageText;
    public GameObject gameOverPanel;
    public Text resultText;
    public Button restartButton;
    public Button mainMenuButton;
    
    // 玩家引用
    public Player localPlayer;
    public Player aiPlayer;
    
    // 游戏管理器引用
    private GameManager gameManager;
    
    // 其他组件引用
    private InputManager inputManager;
    
    // 游戏状态
    private bool isBattleOver = false;
    
    private void Awake()
    {
        // 获取游戏管理器实例
        gameManager = GameManager.instance;
        if (gameManager == null)
        {
            Debug.LogError("[BattleSceneManager] 无法找到GameManager实例");
        }
        
        // 获取输入管理器
        inputManager = FindObjectOfType<InputManager>();
        if (inputManager == null)
        {
            Debug.LogError("[BattleSceneManager] 无法找到InputManager实例");
        }
        
        // 初始隐藏游戏结束面板
        if (gameOverPanel != null)
            gameOverPanel.SetActive(false);
    }
    
    private void Start()
    {
        // 确保找到了所有需要的玩家引用
        FindPlayers();
        
        // 设置玩家的onDeath事件
        SetupPlayerDeathEvents();
        
        // 设置按钮事件
        SetupButtons();
        
        // 显示战斗开始消息
        ShowBattleMessage("战斗开始！");
    }
    
    // 找到并正确分配玩家引用
    private void FindPlayers()
    {
        if (localPlayer == null)
        {
            GameObject localPlayerObj = GameObject.Find("LocalPlayer");
            if (localPlayerObj != null)
                localPlayer = localPlayerObj.GetComponent<Player>();
        }
            
        if (aiPlayer == null)
        {
            GameObject aiPlayerObj = GameObject.Find("AIPlayer");
            if (aiPlayerObj != null)
                aiPlayer = aiPlayerObj.GetComponent<Player>();
        }
            
        // 设置isLocalPlayer属性
        if (localPlayer != null)
            localPlayer.isLocalPlayer = true;
        
        if (aiPlayer != null)
            aiPlayer.isLocalPlayer = false;
    }
    
    // 设置玩家死亡事件
    private void SetupPlayerDeathEvents()
    {
        if (localPlayer != null)
        {
            localPlayer.onDeath.AddListener(() => {
                HandlePlayerDeath(false); // 本地玩家死亡
            });
        }
        
        if (aiPlayer != null)
        {
            aiPlayer.onDeath.AddListener(() => {
                HandlePlayerDeath(true); // AI玩家死亡
            });
        }
    }
    
    // 设置按钮事件
    private void SetupButtons()
    {
        if (restartButton != null)
            restartButton.onClick.AddListener(RestartBattle);
            
        if (mainMenuButton != null)
            mainMenuButton.onClick.AddListener(ReturnToMainMenu);
    }
    
    // 处理玩家死亡
    private void HandlePlayerDeath(bool playerWon)
    {
        if (isBattleOver) return;
        
        isBattleOver = true;
        
        // 禁用输入
        if (inputManager != null)
            inputManager.enabled = false;
        
        // 显示游戏结束面板
        if (gameOverPanel != null)
            gameOverPanel.SetActive(true);
            
        // 设置结果文本
        if (resultText != null)
            resultText.text = playerWon ? "你赢了！" : "你输了！";
    }
    
    // 显示战斗消息
    public void ShowBattleMessage(string message)
    {
        if (battleMessageText != null)
            battleMessageText.text = message;
    }
    
    // AI玩家受伤
    public void DamageAIPlayer(int damage)
    {
        if (aiPlayer != null && !isBattleOver)
            aiPlayer.TakeDamage(damage);
    }
    
    // 本地玩家受伤
    public void DamageLocalPlayer(int damage)
    {
        if (localPlayer != null && !isBattleOver)
            localPlayer.TakeDamage(damage);
    }
    
    // 重新开始战斗
    public void RestartBattle()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
    
    // 返回主菜单
    public void ReturnToMainMenu()
    {
        // 清理从Excel导入的单词数据
        ExcelFileImporter.ClearImportedWords();
        
        // 加载主菜单场景
        SceneManager.LoadScene("Login");
    }
} 